/********************************************************************************************************
/*
/*    cFile.cpp
/*
/********************************************************************************************************/

#include "stdafx.h"
#include "cFile.h"
#include "wchar.h"


//=======================================================================================================
// PROTOTYPES
//=======================================================================================================

static FILE * open_text_file_for_reading ( const wchar_t * pwFile );
static FILE * open_text_file_for_writing ( const wchar_t * pwFile );

//=======================================================================================================
// CODE
//=======================================================================================================


//-------------------------------------------------------------------------------------------------------
// GET LINE
//-------------------------------------------------------------------------------------------------------
bool cFileIn :: get_line ( cStrW * s )
{
	wint_t c, iQtyRead = 0;

	while ( WEOF != ( c = getwc ( h ) ) )
	{
		iQtyRead ++;

		if ( 0x2028 == c )  // unicode line separator
			break;

		else if ( 0x0A == c )
			break;

		else if ( 0x0D == c )
			;

		else
		{
			*s += static_cast<wchar_t> ( c );
		}
	}

	return iQtyRead ? true : false;
}



//-------------------------------------------------------------------------------------------------------
// CONSTRUCTOR
//-------------------------------------------------------------------------------------------------------
cFileIn :: cFileIn ( const wchar_t * pName ) : sName ( pName )
{
	h = open_text_file_for_reading ( pName );
}


//-------------------------------------------------------------------------------------------------------
// DESTRUCTOR
//-------------------------------------------------------------------------------------------------------
cFileIn :: ~cFileIn ()
{
	if ( h )
		fclose ( h );
}


//-------------------------------------------------------------------------------------------------------
// OPEN TEXT FILE FOR READING
// This will automatically open a file correctly 
// REGARDLESS OF WHETHER IT'S ANSI, UTF-8, or UTF-16
//-------------------------------------------------------------------------------------------------------
static FILE * open_text_file_for_reading ( const wchar_t * pwFile )
{
	FILE * h;

	if ( 0 != _wfopen_s ( & h, pwFile, L"rt, ccs=UTF-8" ) )
		return 0;

	else
		return h;
}



//-------------------------------------------------------------------------------------------------------
// CONSTRUCTOR
//-------------------------------------------------------------------------------------------------------
cFileOut :: cFileOut ( const wchar_t * pName ) : sName ( pName )
{
	h = open_text_file_for_writing ( pName );
}


//-------------------------------------------------------------------------------------------------------
// DESTRUCTOR
//-------------------------------------------------------------------------------------------------------
cFileOut :: ~cFileOut ()
{
	if ( h )
		fclose ( h );
}



//-------------------------------------------------------------------------------------------------------
// OPEN TEXT FILE FOR WRITING
// This will automatically open a file correctly 
// REGARDLESS OF WHETHER IT'S ANSI, UTF-8, or UTF-16
//-------------------------------------------------------------------------------------------------------
static FILE * open_text_file_for_writing ( const wchar_t * pwFile )
{
	FILE * h;

	if ( 0 != _wfopen_s ( & h, pwFile, L"wt, ccs=UTF-8" ) )
		return 0;

	else
		return h;
}